package com.pervasive.geoTagging;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;

public class BalloonOverlay extends FrameLayout {

	public BalloonOverlay(Context context) {
		super(context);
		setPadding(10, 0, 10, 0);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.balloon_overlay, layout);
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		progressBar = (ProgressBar)v.findViewById(R.id.progress_bar);
		ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				layout.setVisibility(GONE);
				MarkerList.queryThread.interrupt();
			}
		});
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;
		addView(layout, params);
	}
	
	public void setData(OverlayItem item) {
		layout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			title.setText("Album: "+item.getTitle());
			progressBar.setVisibility(View.VISIBLE);
		}
		else
			title.setText(getResources().getString(R.string.erroreTitoloAlbum));
		title.setVisibility(VISIBLE);
	}
	
	public ProgressBar getProgressBar(){
		return progressBar;
	}
	
	public void setMax(int max){
		progressBar.setMax(max);
		progressBar.setVisibility(View.VISIBLE);
	}
	
	public void setProgress(int prog){
		progressBar.setProgress(prog);
		progressBar.setVisibility(View.VISIBLE);
	}

	public String getTitleForQuery() {
		return title.getText().toString().replace(" ", "+").toLowerCase().substring(7);
	}
	
	public void setText(String newText){
		title.setText(newText);
	}
	
	private LinearLayout layout;
	private TextView title;
	private ProgressBar progressBar;
}